import Request from 'utils/request';
import SystemUtil from 'utils/systemUtil';

import GoodsPaymentApi from './goodsPaymentApi';

import ShopDetail from 'models/shopMall/shopDetail';
import ShopGoods from 'models/shopMall/shopGoods';
import ShopEvaluate from 'models/shopMall/shopEvaluate';
import BuyGoosRecord from 'models/shopMall/buyGoosRecord';

const evaluateTypeObj = {
  all: 1,
  hasImg: 2,
  noImg: 3
};

export default class ShopMallApi {
  static getShopMallData(postData) {
    const apiName = 'api/wehcatshop/shopInfo';

    let type = postData.branchId ? 'branch' : 'partner';

    return Request.post(apiName, postData).then(res => {
      let shopDetail = res.data;
      shopDetail = new ShopDetail(shopDetail, type, postData);
      return shopDetail;
    });
  }

  static getShopGoods(postData) {
    const apiName = 'api/wehcatshop/shopInfoGoodsTypes';

    return Request.post(apiName, postData).then(res => {
      let shopGoods = new ShopGoods(res.data);
      return shopGoods;
    });
  }

  static getShopEvaluateList(shopData, type, pagination) {
    const apiName = 'api/wehcatshop/shopAppraiseList';

    let postData = {
      limit: pagination.pageSize,
      page: pagination.currentPage,
      type: evaluateTypeObj[type]
    };
    if (shopData.type === 'branch') {
      postData.branchId = shopData.branchId;
    } else {
      postData.partnerId = shopData.partnerId;
    }

    return Request.post(apiName, postData).then(res => {
      let evaluates = res.data;
      if (SystemUtil.isEmpty(evaluates)) {
        evaluates = [];
      }
      let evaluateList = [];
      for (let i = 0; i < evaluates.length; i++) {
        let evaluate = evaluates[i];
        evaluate = new ShopEvaluate(evaluate);
        evaluateList.push(evaluate);
      }

      pagination.totalCount = res.rows;
      return evaluateList;
    });
  }

  static goodsToCar(goodsModelData) {
    const apiName = 'api/buycarlog/save';

    let postData = goodsModelData.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static preBuy(goodsModelData) {
    const apiName = 'api/visitwanttobuylog/save';

    let postData = goodsModelData.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static buyGoods(goodsModelData, lnglat) {
    let goods = goodsModelData.getBuyGoods();
    return GoodsPaymentApi.payGoodsList([goods], lnglat, 'SPXQ');
  }

  static toGoodsBalance(buyGoodsRecord, lnglat) {
    let goodsList = buyGoodsRecord.getGoodsList();
    return GoodsPaymentApi.payGoodsList(goodsList, lnglat, 'DPXQ');
  }

  static getBuyGoodsRecord(postData) {
    const apiName = 'api/wehcatshop/getVisitWantToBuyLogForToday';

    return Request.post(apiName, postData).then(res => {
      let buyRecord = new BuyGoosRecord(res.data);
      return buyRecord;
    });
  }
}
